<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of home
 *
 * @author peerawatj20
 */
class Home extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('repair_model');
        $this->load->model('catalog');
    }

    private $license_code = '';
    private $order_type = '';
    private $s_day = '';
    private $s_month = '';
    private $s_year = '';
    private $e_day = '';
    private $e_month = '';
    private $e_year = '';
    private $start_date = '';
    private $end_date = '';

    public function index() {

        $this->session->unset_userdata('search_license');
        $this->session->unset_userdata('search_order_type');
        $this->session->unset_userdata('search_sday');
        $this->session->unset_userdata('search_smonth');
        $this->session->unset_userdata('search_syear');
        $this->session->unset_userdata('search_eday');
        $this->session->unset_userdata('search_emonth');
        $this->session->unset_userdata('search_eyear');

        $config['base_url'] = base_url() . "/repair/home/search";
        $config['total_rows'] = $this->repair_model->count();
        $config['per_page'] = 10;
        $config['uri_segment'] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);

        $this->pagination->initialize($config);

        $result = $this->repair_model->search_list_repair();
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);

        $this->_set_form();
        $this->load->view('repair/index', $this->data);
    }

    private function _set_form() {
        $utility = new Config_utility();
        $this->data['txt_license_code'] = form_input('txtLicenseCode', $this->license_code, 'class="form-control" placeholder="ทะเบียน"');
        $this->data['cmb_order_type'] = form_dropdown('cmbOrderType', $this->catalog->list_repair_type(), $this->order_type, 'class="form-control" id="cmbOrderType" style="width:150px"');
        $this->data['cmb_start_day'] = form_dropdown('cmbStartDay', $utility->list_day(), $this->s_day, 'class="form-control"');
        $this->data['cmb_start_month'] = form_dropdown('cmbStartMonth', $utility->list_month(), $this->s_month, 'class="form-control"');
        $this->data['cmb_start_year'] = form_dropdown('cmbStartYear', $utility->list_year(), $this->s_year, 'class="form-control"');
        $this->data['cmb_end_day'] = form_dropdown('cmbEndDay', $utility->list_day(), $this->e_day, 'class="form-control"');
        $this->data['cmb_end_month'] = form_dropdown('cmbEndMonth', $utility->list_month(), $this->e_month, 'class="form-control"');
        $this->data['cmb_end_year'] = form_dropdown('cmbEndYear', $utility->list_year(), $this->e_year, 'class="form-control"');
    }

    public function search() {
        //Clean Session for searching and it come from search btn.
        if ($this->input->post('action') == 'search') {
            $this->session->unset_userdata('search_license');
            $this->session->unset_userdata('search_order_type');
            $this->session->unset_userdata('search_sday');
            $this->session->unset_userdata('search_smonth');
            $this->session->unset_userdata('search_syear');
            $this->session->unset_userdata('search_eday');
            $this->session->unset_userdata('search_emonth');
            $this->session->unset_userdata('search_eyear');

            $this->license_code = $this->input->post('txtLicenseCode');
            $this->order_type = $this->input->post('cmbOrderType');
            $this->s_day = $this->input->post('cmbStartDay');
            $this->s_month = $this->input->post('cmbStartMonth');
            $this->s_year = $this->input->post('cmbStartYear');
            $this->e_day = $this->input->post('cmbEndDay');
            $this->e_month = $this->input->post('cmbEndMonth');
            $this->e_year = $this->input->post('cmbEndYear');

            $this->session->set_userdata('search_license', $this->license_code);
            $this->session->set_userdata('search_order_type', $this->order_type);
            $this->session->set_userdata('search_sday', $this->s_day);
            $this->session->set_userdata('search_smonth', $this->s_month);
            $this->session->set_userdata('search_syear', $this->s_year);
            $this->session->set_userdata('search_eday', $this->e_day);
            $this->session->set_userdata('search_emonth', $this->e_month);
            $this->session->set_userdata('search_eyear', $this->e_year);
        } else {
            //set value for pagination.
            $this->license_code = $this->session->userdata('search_license');
            $this->order_type = $this->session->userdata('search_order_type');
            $this->s_day = $this->session->userdata('search_sday');
            $this->s_month = $this->session->userdata('search_smonth');
            $this->s_year = $this->session->userdata('search_syear');
            $this->e_day = $this->session->userdata('search_eday');
            $this->e_month = $this->session->userdata('search_emonth');
            $this->e_year = $this->session->userdata('search_eyear');
        }

        if ($this->s_day != '' && $this->s_month != '' && $this->s_year != '') {
            $this->start_date = $this->s_year . "/" . $this->s_month . "/" . $this->s_day;
        }

        if ($this->e_day != '' && $this->e_month != '' && $this->e_year != '') {
            $this->end_date = $this->e_year . "/" . $this->e_month . "/" . $this->e_day;
        }

        $config['base_url'] = base_url() . "/repair/home/search";
        $config['total_rows'] = $this->repair_model->count($this->license_code, $this->order_type, $this->start_date, $this->end_date);
        $config['per_page'] = 10;
        $config["uri_segment"] = 4;
        $utility = new Config_form();
        $config = $utility->form_pagination($config);
        $this->pagination->initialize($config);

//        echo $this->db->last_query();
//        exit();
        $result = $this->repair_model->search_list_repair($this->license_code, $this->order_type, $this->start_date, $this->end_date, $config['per_page'], $this->uri->segment(4));
        $this->data['result_list'] = $result;
        $this->data['num_list'] = $this->uri->segment(4);
        $this->_set_form();
        $this->load->view('repair/index', $this->data);
    }

}
